package com.ossjk.oa.system.service.impl;

import com.ossjk.core.util.CommonUtil;
import com.ossjk.oa.system.entity.Post;
import com.ossjk.oa.system.mapper.PostMapper;
import com.ossjk.oa.system.service.IPostService;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 岗位表 服务实现类
 * </p>
 *
 * @author chair
 * @since 2019-01-23
 */
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IPostService {
	public Post findPostByEid(@Param("eid") String eid) {
		return baseMapper.findByEid(eid);
	};

	@Override
	public List<Post> selectSubByPid(String pid) {
		List<Post> posts = this.selectList(Condition.create().orderBy("level").orderBy("sort"));
		return filterSub(pid, posts);
	}

	@Override
	public List<String> selectSubIdByPid(String pid) {
		List<Post> posts = this.selectList(Condition.create().orderBy("level").orderBy("sort"));
		return filterSubId(pid, posts);
	}

	/**
	 * 过滤子集
	 * 
	 * @param id
	 * @param list
	 * @param list2
	 */
	private List<Post> filterSub(String pid, List<Post> posts) {
		List<Post> subs = null;
		if (!CommonUtil.isBlank(posts)) {
			for (Post post : posts) {
				if (pid.equals(post.getPid())) {
					if (CommonUtil.isBlank(subs)) {
						subs = new ArrayList<Post>();
					}
					subs.add(post);
					List<Post> list = filterSub(post.getId(), posts);
					if (!CommonUtil.isBlank(list)) {
						subs.addAll(list);
					}
				}
			}

		}
		return subs;

	}

	/**
	 * 过滤子集id
	 * 
	 * @param id
	 * @param list
	 * @param list2
	 */
	private List<String> filterSubId(String pid, List<Post> posts) {
		List<String> subs = null;
		if (!CommonUtil.isBlank(posts)) {
			for (Post post : posts) {
				if (CommonUtil.isBlank(subs)) {
					subs = new ArrayList<String>();
				}
				if (pid.equals(post.getPid())) {
					subs.add(post.getId());
					List<String> list = filterSubId(post.getId(), posts);
					if (!CommonUtil.isBlank(list)) {
						subs.addAll(list);
					}
				}
			}
		}
		return subs;

	}
}
